<?php
namespace App\Http\Controllers\Api\V2\Common;

use App\Http\Controllers\ApiController;
use App\Http\Controllers\Business\App\Common\Common;
use App\Http\Controllers\Business\App\Today\Today;
use Dingo\Api\Http\Request;

class CommonController extends ApiController
{
    //获取已开通的省份，城市
    public function getAllOpenedCities() {
        if($this->request->get('versioncode') >= 15) {
            $cityResult = Common::getCitiesByV15Version();
        } else {
            $cityResult = Common::getAllOpenedCities();
        }
        if($cityResult['status'] == 'failed') {
            return $this->responseError('获取城市失败');
        }
        $data = $cityResult['data'];
        return $this->responseSuccessData($data);
    }
    //获取商家一级，二级分类
    public function getClass() {
        $classResult = Common::getClass();
        if($classResult['status'] == 'failed') {
            return $this->responseError('获取分类失败');
        }
        $data = $classResult['data'];
        return $this->responseSuccessData($data);
    }
    //获取商家夜场分类
    public function getNightEventClass() {
        $result = Common::getNightEventClass();
        if($result['status'] == 'failed') {
            return $this->responseError('获取分类失败');
        }
        $message = $result['message'];
        $data = $result['data'];
        return $this->responseSuccess($message, $data);
    }
    //通过城市id获取区县和商圈
    public function getDistrictAndBusinessArea(Request $request) {
        if(!$request->has('cityid')) {
            return $this->responseError('无效参数');
        }
        $cityId = $request->get('cityid');
        $districtResult = Common::getDistrictAndBusinessArea($cityId);
        if($districtResult['status'] == 'failed') {
            return $this->responseError($districtResult['message']);
        }
        $message = $districtResult['message'];
        $data = $districtResult['data'];
        return $this->responseSuccess($message, $data);
    }
    //通过ip获取高德地图的经纬度
    public function getLongtitudeAndLatitudeByIp(Request $request) {
        $ip = '182.101.60.75';
        //$ip = $request->ip();
        $result = Common::getPositionByIp($ip);
        dd($result);
    }
    //获取oss帐号
    public function getStsOssAuth() {
        $result = Common::getStsOssAuth();
        return $this->responseSuccessData($result['data']);
    }
    //andriod 版本更新
    public function androidVersion() {
        if(env('WEB_URL') == 'https://test2.fbw-china.com/') {
            //测试环境
            $data = [
                'versioncode' => 12,
                'versionname' => '2.2.0',
                'isfroce' => 1,
                'updatemsg' => "1.查看大图时增加了水印;\n2.对商家详情图片优化;\n3.优化了用户体验，修复了已知的bug\n",
                'downloadurl' => 'http://file.fbw-china.com/download/fbw.apk'
            ];
        } else {
            //正式环境
            $data = [
                'versioncode' => 231,
                'versionname' => '2.3.1',
                'isfroce' => 0,
                'updatemsg' => "1.查看大图时增加了水印;\n2.对商家详情图片优化;\n3.优化了用户体验，修复了已知的bug\n",
                'downloadurl' => 'http://file.fbw-china.com/download/fbw.apk'
            ];
        }
        return $this->responseSuccessData($data);
    }
    public function iosVersion() {
        if(env('WEB_URL') == 'https://test2.fbw-china.com/') {
            //测试环境
            $data = [
                'versioncode' => 15,
                'versionname' => '300',
                'isfroce' => 1,
                'updatemsg' => "1.查看大图时增加了水印;\n2.对商家详情图片优化;\n3.优化了用户体验，修复了已知的bug\n",
                //'downloadurl' => 'https://itunes.apple.com/cn/app/id1172353317',
                'downloadurl'=>'https://itunes.apple.com/app/apple-store/id1172353317?pt=118361877&ct=fbwp&mt=8'
            ];
        } else {
            //正式环境
            $data = [
                'versioncode' => 231,
                'versionname' => '231',
                'isfroce' => 0,
                'updatemsg' => "1.查看大图时增加了水印;\n2.对商家详情图片优化;\n3.优化了用户体验，修复了已知的bug;\n**如无法更新请在App Store中点击更新选项查看更新",
                'downloadurl' => 'https://itunes.apple.com/cn/app/id1172353317'
            ];
        }
        return $this->responseSuccessData($data);
    }
    //意见反馈
    public function feedback() {
        $userId = $this->userId;
        $content = $this->request->get('content');
        $osVersion = $this->request->get('osversion');
        $phoneType = $this->request->get('phonetype');
        $versionCode = $this->request->get('versioncode');
        if($this->request->has('cityid')){
            $cityId = $this->request->get('cityid');
        }else{
            $cityId = 1;
        }
        $result = Common::feedback($userId, $content, $osVersion, $phoneType, $versionCode,$cityId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //推广二维码
    public function getExtensionQrcode() {
        $string = '刘胖子测试';
        return $this->responseSuccessData($string);
    }

    //app启动页
    public function appStartPage() {
        if($this->request->has('cityid')) {
            $cityId = $this->request->get('cityid');
        } else {
            $cityId = 1;
        }
        $data = [];
        $result = Today::getAppStartList($cityId);
        if($result['status'] == 'failed') {
            return $this->responseSuccessData($data);
        }
        $today = 0;
        $tomorrow = 0;
        $thirdDay = 0;
        $lists = $result['data'];
        $ids = [];
        foreach($lists as $list) {
            $startTime = $list['starttime'];
            $lastTime = $list['lasttime'];
            if(($startTime<= date('Y-m-d')) && ($lastTime>=date('Y-m-d')) && ($today == 0)) {
                array_push($data, [
                                'name' => $list['name'],
                                'picurl' => $list['picurl'],
                                'type' => $list['type'],
                                'content' => $list['content'],
                                'status' => $list['status'],
                                'launchtime' => date('Y-m-d'),
                ]);
                $today = 1;
                array_push($ids, $list['id']);
            }
            if(($startTime<= date('Y-m-d', strtotime("+1days"))) && ($lastTime>=date('Y-m-d', strtotime("+1days"))) && ($tomorrow == 0)) {
                array_push($data, [
                    'name' => $list['name'],
                    'picurl' => $list['picurl'],
                    'type' => $list['type'],
                    'content' => $list['content'],
                    'status' => $list['status'],
                    'launchtime' => date('Y-m-d', strtotime("+1days")),
                ]);
                $tomorrow = 1;
                array_push($ids, $list['id']);
            }
            if(($startTime<= date('Y-m-d', strtotime("+2days"))) && ($lastTime>=date('Y-m-d', strtotime("+2days"))) && ($thirdDay == 0)) {
                array_push($data, [
                    'name' => $list['name'],
                    'picurl' => $list['picurl'],
                    'type' => $list['type'],
                    'content' => $list['content'],
                    'status' => $list['status'],
                    'launchtime' => date('Y-m-d', strtotime("+2days")),
                ]);
                $thirdDay = 1;
                array_push($ids, $list['id']);
            }
        }
        $updateAppStartStatus = Today::updateAppStartStatus($cityId, $ids);
        return $this->responseSuccessData($data);
    }
}